<?php
/**
 * Created By virus_cms
 * Author: Virus
 * Date: 2020/3/23
 * Time: 13:59
 */

namespace app\common\bussiness;


use app\common\model\mysql\Category;

class CategoryBusiness
{
    protected $model = null;

    public function __construct()
    {
        $this->model = new Category();
    }

    public function getList()
    {
        $result = $this->model->getList();
        if (empty($result)) {
            return [];
        }

        return $result->toArray();
    }

    public function getPidCategory($id)
    {
        $field  = "id,title";
        $where  = [
            "status" => 1,
            "id"     => $id,
        ];
        $order  = ["order" => "desc"];
        $result = $this->model->getPidCategory($where, $order, $field);
        if (empty($result)) {
            return [];
        }

        return $result->toArray();
    }

    public function addCategory($data)
    {
        $result = $this->model->addData($data);
        if (!$result) {
            throw new \think\Exception("添加失败");
        }

        return true;
    }

    public function getCategoryOneById($id)
    {
        $result = $this->model->getOneDataById($id);
        if (empty($result)) {
            return [];
        }

        return $result;
    }

    public function updateCategory($data)
    {
        $result = $this->model->updateCategory($data);
        if (false === $result) {
            throw new \think\Exception("分类编辑失败");
        }

        return true;
    }

    public function deleteById($id)
    {
        $result = $this->model->deleteOneData($id);
        if (!$result) {
            throw new \think\Exception("分类删除失败");
        }

        return $result;
    }
}